<?php
class ManagePromotionHotelForm extends Form
{
    function ManagePromotionHotelForm()
    {
        Form::Form("ManagePromotionHotelForm");

        $this->link_css("style/hotel.css");
        $this->link_css("style/paging.css");
    }

    function draw()
    {
        global $display;
        $display->add("WEB_ROOT", WEB_ROOT);
        $display->add("WEB_DIR", WEB_DIR);
        $this->beginForm(true);

        $item_perpage = 20;
        $con_arr = array();
        //Lọc theo tên ks
        $hotel_name = Url::get("keyword", '');
        $display->add('hotel_name', $hotel_name);
        if ($hotel_name) {
            $listId = array();
            $listIdHotel = Db_Hotel::getHotelsId('', '(`name` LIKE "%' . DB::escape($hotel_name) . '%")');
            if ($listIdHotel !== false && sizeof($listIdHotel) > 0) {
                foreach ($listIdHotel as $item) {
                    $listId[] = $item['id'];
                }
            }
            $hotelId = join(',', $listId);
            if ($hotelId == "") {
                $hotelId = "0";
            }
            $con_arr[] = '(`hotel_id` IN (' . $hotelId . '))';
            $condition = implode($con_arr, ' AND ');
            $order = "";
        } else {
            $condition = "";
            $order = " `hotel_id` ";
        }
        $total_promotion_hotels = Db_Promotion::getPromotionHotel("*", "", $condition, $order);
        $total = sizeof($total_promotion_hotels);
        $display->add('paging', ECPaging::paging($limit, $total, $item_perpage, 10, 'page_no', true, 'Chính sách'));
        $promotion_hotels = Db_Promotion::getPromotionHotel("*", $limit, $condition);


        foreach ($promotion_hotels as &$ph) {
            if (($temp = Db_Hotel::getHotelById($ph['hotel_id'])) !== false) {
                $ph['name_hotel'] = $temp['name'];
            }
            if (($temp2 = Db_Promotion::getPromotionFilterById($ph['type'])) !== false) {
                $ph['name_type'] = $temp2[$ph['type']]['name'];
            }
            if ($ph['room_id'] == 0) {
                $ph['name_room'] = "Toàn bộ khách sạn";
            } else {
                if (($temp3 = Db_Room::getRoomById($ph['room_id'])) !== false) {
                    $ph['name_room'] = "Phòng " . $temp3['name'];
                }
            }
            if ($ph["time_end"] <= TIME_NOW) {
                $ph["out_of_date"] = 1;
            } else {
                $ph["out_of_date"] = 0;
            }
            $ph["time_start"] = date("d/m/Y", $ph["time_start"]);
            $ph["time_end"] = date("d/m/Y", $ph["time_end"]);
        }
        $display->add('promotion_hotels', $promotion_hotels);
        $display->add('can_edit', User::have_permit('hotel_edit'));
        $display->add('can_add', User::have_permit('hotel_add'));
        $display->add('can_delete', User::have_permit('hotel_delete'));
        $display->add('href', urldecode(Url::build_all(array('cmd', 'act'))));

        if (User::is_admin()) {
            $display->add('is_admin', true);
        }
        $display->output('ManagePromotionHotel');
        $this->endForm();
    }

    function on_submit()
    {
        $keyword = Url::get("hotel_name");
        if ($keyword !== '') {
            Url::redirect('manage_promotion_hotel.html?keyword=' . $keyword);
        } else {
            Url::redirect('manage_promotion_hotel.html');
        }
    }
}

?>